package com.avic.rong.transform.activity;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.avic.rong.R;
import com.avic.rong.utils.DensityUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Creater: Created by wangyz on 2019/9/7.
 * Email: 936804097@qq.com
 */
public class SearchFilterViewTransform extends LinearLayout implements View.OnClickListener {


    private Context context;
    private RadioButton rb_sort_zh;
    private RadioButton rb_sort_time;
    private RadioButton rb_sort_end_time;
    private RadioButton rb_sort_filter;
    private List<RadioButton> radioButtonList = new ArrayList<>();
    boolean timeFlag = true;//倒叙
    boolean endTimeFlag = true;
    int sort = 0;//默认为0

    public SearchFilterViewTransform(Context context) {
        this(context, null);
    }

    public SearchFilterViewTransform(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public SearchFilterViewTransform(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        View myView = LayoutInflater.from(context).inflate(R.layout.view_search_filter_transform, null);
        rb_sort_zh = myView.findViewById(R.id.rb_sort_zh);
        rb_sort_time = myView.findViewById(R.id.rb_sort_time);
        rb_sort_end_time = myView.findViewById(R.id.rb_sort_end_time);
        rb_sort_filter = myView.findViewById(R.id.rb_sort_filter);
        radioButtonList.add(rb_sort_zh);
        radioButtonList.add(rb_sort_time);
        radioButtonList.add(rb_sort_end_time);

        int width = DensityUtils.getWindowWidth(context) / 4;
        RadioGroup.LayoutParams layoutParams = new RadioGroup.LayoutParams(width, DensityUtils.dp2px(context, 40));
        rb_sort_zh.setLayoutParams(layoutParams);
        rb_sort_time.setLayoutParams(layoutParams);
        rb_sort_end_time.setLayoutParams(layoutParams);
        rb_sort_filter.setLayoutParams(layoutParams);
        setBackgroundColor(ContextCompat.getColor(context, R.color.transparent));
        addView(myView);

        rb_sort_zh.setOnClickListener(this);
        rb_sort_time.setOnClickListener(this);
        rb_sort_end_time.setOnClickListener(this);
        rb_sort_filter.setOnClickListener(this);
    }



    public void hiddenEndTime(){
        rb_sort_end_time.setVisibility(View.INVISIBLE);
    }



    public void hiddenFilter(){
        rb_sort_filter.setVisibility(View.INVISIBLE);
    }


    @Override
    public void onClick(View view) {
        int viewId = view.getId();
        switch (viewId) {
            case R.id.rb_sort_zh:
                sort = 0;
                switchRadioButton(rb_sort_zh);
                filterInterface.clickSort(sort);
                break;
            case R.id.rb_sort_time:
                switchRadioButton(rb_sort_time);
                filterInterface.clickSort(sort);
                break;
            case R.id.rb_sort_end_time:
                switchRadioButton(rb_sort_end_time);
                filterInterface.clickSort(sort);
                break;
            case R.id.rb_sort_filter://过滤
                filterInterface.openFilter();
                break;
        }

    }


    public void switchRadioButton(int sort) {
        this.sort = sort;
        if (sort == 0) {
            switchRadioButton(rb_sort_zh);
        } else if (sort == 1) {
            timeFlag = true;
            switchRadioButton(rb_sort_time);
        } else if (sort == 2) {
            timeFlag = false;
            switchRadioButton(rb_sort_time);
        } else if (sort == 3) {
            endTimeFlag = true;
            switchRadioButton(rb_sort_end_time);
        } else if(sort == 4){
            endTimeFlag = false;
            switchRadioButton(rb_sort_end_time);
        }
    }

    private void switchRadioButton(RadioButton tmp) {
        for (RadioButton radioButton : radioButtonList) {
            if (radioButton.getId() == tmp.getId()) {
                if (radioButton.getId() == R.id.rb_sort_time) {
                    if (timeFlag) {
                        sort = 1;
                        setSortTimeDrawable(rb_sort_time, R.mipmap.up_time_sort2x);
                    } else {
                        sort = 2;
                        setSortTimeDrawable(rb_sort_time, R.mipmap.down_time_sort2x);
                    }
                    timeFlag = !timeFlag;
                }
                if(radioButton.getId() == R.id.rb_sort_end_time){
                    if (endTimeFlag) {
                        sort = 3;
                        setSortTimeDrawable(rb_sort_end_time, R.mipmap.up_time_sort2x);
                    } else {
                        sort = 4;
                        setSortTimeDrawable(rb_sort_end_time, R.mipmap.down_time_sort2x);
                    }
                    endTimeFlag = !endTimeFlag;
                }

                radioButton.setSelected(true);
            } else {
                if (radioButton.getId() == R.id.rb_sort_time) {
                    setSortTimeDrawable(radioButton, R.mipmap.goods_sort_time_sort);
                }
                if (radioButton.getId() == R.id.rb_sort_end_time) {
                    setSortTimeDrawable(radioButton, R.mipmap.goods_sort_time_sort);
                }
                radioButton.setSelected(false);
            }
        }
    }

    private FilterInterface filterInterface;

    public void setFilterInterface(FilterInterface filterInterface) {
        this.filterInterface = filterInterface;
    }

    public interface FilterInterface {
        public void clickSort(int sort);

        public void openFilter();
    }

    private void setSortTimeDrawable(RadioButton rb_sort_time, int p) {
        Drawable downTime = getResources().getDrawable(p);
        downTime.setBounds(0, 0, downTime.getMinimumWidth(), downTime.getMinimumHeight());
        rb_sort_time.setCompoundDrawables(null, null, downTime, null);
    }

}
